<?php

App::uses('AppController', 'Controller');
App::uses('AuthComponent', 'Controller/Component');
/**
 * Users Controller
 *
 * @property User $User
 */
class UsersController extends AppController {
    
//    public $belongsTo = array('Role');
//    public $actAs = array('Acl' => array('type' => 'requester'));
//    
//    public function parentNode(){
//        if(!$this->id && empty($this->data)){
//            return null;
//        }
//        if(isset($this->data['User']['role_id'])){
//            $roleId = $this->data['User']['role_id'];
//        }else{
//            $roleId = $this->field('role_id');
//        }
//        if(!roleId){
//            return null;
//        }else{
//            return array('Role' =>array('id'=>$roleId));
//        }
//    }

    /**
     * index method
     *
     * @return void
     */
    public function index() {
        $this->User->recursive = 0;
        $this->set('users', $this->paginate());
    }

    /**
     * view method
     *
     * @throws NotFoundException
     * @param string $id
     * @return void
     */
    public function view($id = null) {
        if (!$this->User->exists($id)) {
            throw new NotFoundException('Invalid user', 'default', array('class' => 'alert alert-danger'));
        }
        $options = array('conditions' => array('User.' . $this->User->primaryKey => $id));
        $this->set('user', $this->User->find('first', $options));
    }

    /**
     * add method
     *
     * @return void
     */
    public function add() {
        if ($this->request->is('post') || $this->request->is('put')) {
            $this->User->Behaviors->attach('Passwordable');
            $this->User->create();
            if ($this->User->save($this->request->data)) {
                $this->Session->setFlash('The user has been saved', 'default', array('class' => 'alert alert-info'));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash('The user could not be saved. Please, try again.', 'default', array('class' => 'alert alert-danger'));
                unset($this->request->data['User']['pwd']);
    unset($this->request->data['User']['pwd_repeat']);
            }
        }
        $roles = $this->User->Role->find('list');
        $this->set(compact('roles'));
    }

    /**
     * edit method
     *
     * @throws NotFoundException
     * @param string $id
     * @return void
     */
    public function edit($id = null) {
        if (!$this->User->exists($id)) {
            throw new NotFoundException('Invalid user', 'default', array('class' => 'alert alert-danger'));
        }
        if ($this->request->is('post') || $this->request->is('put')) {
             $this->User->Behaviors->attach('Passwordable', array('require' => false));          
            if ($this->User->save($this->request->data)) {
                $this->Session->setFlash('The user has been saved', 'default', array('class' => 'alert alert-info'));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash('The user could not be saved. Please, try again.', 'default', array('class' => 'alert alert-danger'));
            }
        } else {
            $options = array('conditions' => array('User.' . $this->User->primaryKey => $id));
            $this->request->data = $this->User->find('first', $options);
        }
        $roles = $this->User->Role->find('list');
        $this->set(compact('roles'));
    }

    /**
     * delete method
     *
     * @throws NotFoundException
     * @param string $id
     * @return void
     */
    public function delete($id = null) {
        $this->User->id = $id;
        if (!$this->User->exists()) {
            throw new NotFoundException('Invalid user', 'default', array('class' => 'alert alert-danger'));
        }
        $this->request->onlyAllow('post', 'delete');
        if ($this->User->delete()) {
            $this->Session->setFlash('User deleted', 'default', array('class' => 'alert alert-info'));
            $this->redirect(array('action' => 'index'));
        }
        $this->Session->setFlash('User was not deleted', 'default', array('class' => 'alert alert-danger'));
        $this->redirect(array('action' => 'index'));
    }

    public function beforeFilter() {
        parent::beforeFilter();
        $this->Auth->allow('admin');
    }

    //login for staff and admin
    public function login() {
        if ($this->request->is('post')) {
            $this->Auth->authenticate = array(
                'Form' => array(
                    'scope' => array('NOT' => array('User.role_id' => 1))
                )
            );
            if ($this->Auth->login()) {
                $this->redirect('/staffbooks/index');
                $this->Session->write('Auth.User');
                //debug($this->Session->write('Auth.User'));
                //die;
            } else {
                $this->Session->setFlash('Invalid username or password', 'default', array('class' => 'alert alert-danger'));
            }
        }
    }

    //login for superadmin
    public function admin() {
        if ($this->request->is('post')) {
            $this->Auth->authenticate = array(
                'Form' => array(
                    'scope' => array('User.role_id' => 1)
                )
            );
            if ($this->Auth->login()) {
                $this->redirect('/staffbooks/index');
                $this->Session->write('Auth.User');
                //debug($this->Session->write('Auth.User'));
                //die;
            } else {
                $this->Session->setFlash('Invalid username or password', 'default', array('class' => 'alert alert-danger'));
            }
        }
    }

    public function logout() {
        $this->Session->setFlash('Logged out successfully', 'default', array('class' => 'alert alert-info'));
        $this->redirect($this->Auth->logout());
        //$this->Session->destroy();
    }

    function initDB() {
        $role = & $this->User->Role;
        //Allow super admins to everything
        $role->id = 1;
        $this->Acl->allow($role, 'controllers');

        //allow admin to archive information, update volunteer roster, links to survey, update resources, update staffbook
        $role->id = 2;
        $this->Acl->deny($role, 'controllers');
        $this->Acl->allow($role, 'controllers/Resources');
        $this->Acl->allow($role, 'controllers/Categories');
        $this->Acl->allow($role, 'controllers/Staffbooks');
        $this->Acl->allow($role, 'controllers/Surveys');
        $this->Acl->allow($role, 'controllers/Rosters');
        $this->Acl->allow($role, 'controllers/Users/logout');

        //allow staff to only add and edit on posts and widgets
        $role->id = 3;
        $this->Acl->deny($role, 'controllers');
        $this->Acl->allow($role, 'controllers/Staffbooks');
        $this->Acl->allow($role, 'controllers/Surveys/index');
        $this->Acl->allow($role, 'controllers/Surveys/go');
        $this->Acl->allow($role, 'controllers/Rosters');
        $this->Acl->allow($role, 'controllers/Users/logout');

        //allow volunteer 
        $role->id = 4;
        $this->Acl->deny($role, 'controllers');
        $this->Acl->allow($role, 'controllers/Staffbooks');
        $this->Acl->allow($role, 'controllers/Surveys/index');
        $this->Acl->allow($role, 'controllers/Surveys/go');
        $this->Acl->allow($role, 'controllers/Rosters');
        $this->Acl->allow($role, 'controllers/Users/logout');

        //allow public to only see resources
        $role->id = 5;
        $this->Acl->deny($role, 'controllers');
        $this->Acl->allow($role, 'controllers/Resources/view');
        $this->Acl->allow($role, 'controllers/Signups');
        $this->Acl->allow($role, 'controllers/Nluploads/view');
    }

}
